package com.youthburin.base.util;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

public class DwzResponseUtils {
	private static Logger logger = LoggerFactory.getLogger(DwzResponseUtils.class);
	private final static String DEFAULT_SUCCESS_MESSAGE = "操作成功";
	private final static String DEFAULT_FAIL_MESSAGE = "操作失败";
	private final static String DEFAULT_VALIDATE_FAIL_MESSAGE = "提交的数据没有通过校验规则，请检查!";

	/**
	 * 创建一个操作成功响应，由closeCurrentTab 参数决定是否关闭当前标签页，若不关闭，则刷新当前标签页
	 * 
	 * @param closeCurrentTab
	 *            是否关闭当前标签页 true-关闭 false-不关闭
	 * @return
	 */
	public static DwzResponse createSuccessResponse(boolean closeCurrentTab) {
		return new DwzResponse(DwzResponse.DwzResponseCode.SUCCESS, DEFAULT_SUCCESS_MESSAGE,
				closeCurrentTab ? DwzResponse.CallbackType.CLOSE : null, null, null);
	}

	/**
	 * 创建一个操作成功响应，由closeCurrentTab 参数决定是否关闭当前标签页，若不关闭，则刷新当前标签页<br>
	 * 若指定navTabId，则刷新相应ID的标签页
	 * 
	 * @param closeCurrentTab
	 *            是否关闭当前标签页 true-关闭 false-不关闭
	 * @param navTabId
	 *            被刷新的其它标签页ID
	 * @return
	 */
	public static DwzResponse createSuccessResponse(boolean closeCurrentTab, String navTabId) {
		return new DwzResponse(DwzResponse.DwzResponseCode.SUCCESS, DEFAULT_SUCCESS_MESSAGE,
				closeCurrentTab ? DwzResponse.CallbackType.CLOSE : null, null, navTabId);
	}

	/**
	 * 创建一个操作成功响应，由closeCurrentTab 参数决定是否关闭当前标签页，若不关闭，则刷新当前标签页
	 * 
	 * @param message
	 *            返回到页面时要显示的消息
	 * @param closeCurrentTab
	 *            是否关闭当前标签页 true-关闭 false-不关闭
	 * @return
	 */
	public static DwzResponse createSuccessResponse(String message, boolean closeCurrentTab) {
		return new DwzResponse(DwzResponse.DwzResponseCode.SUCCESS, message,
				closeCurrentTab ? DwzResponse.CallbackType.CLOSE : null, null, null);
	}

	/**
	 * 创建一个操作成功响应，由closeCurrentTab 参数决定是否关闭当前标签页，若不关闭，则刷新当前标签页<br>
	 * 若指定navTabId，则刷新相应ID的标签页
	 * 
	 * @param message
	 *            返回到页面时要显示的消息
	 * @param closeCurrentTab
	 *            是否关闭当前标签页 true-关闭 false-不关闭
	 * @param navTabId
	 *            被刷新的其它标签页ID
	 * @return
	 */
	public static DwzResponse createSuccessResponse(String message, boolean closeCurrentTab, String navTabId) {
		return new DwzResponse(DwzResponse.DwzResponseCode.SUCCESS, message,
				closeCurrentTab ? DwzResponse.CallbackType.CLOSE : null, null, navTabId);
	}

	public static DwzResponse createFailResponse() {
		return new DwzResponse(DwzResponse.DwzResponseCode.FAIL, DEFAULT_FAIL_MESSAGE, null, null, null);
	}

	public static DwzResponse createFailResponse(List<ObjectError> errors) {
		if (logger.isDebugEnabled()) {
			if (errors != null && !errors.isEmpty()) {
				logger.debug("存在如下校验错误:");
				for (ObjectError error : errors) {
					if (FieldError.class.isAssignableFrom(error.getClass())) {
						logger.info("FieldError->" + error.getObjectName() + "." + ((FieldError) error).getField()
								+ "：[" + ((FieldError) error).getRejectedValue() + "]" + error.getDefaultMessage());
					} else {
						logger.info("ObjectError->" + error.getObjectName() + "：" + error.getDefaultMessage());
					}

				}
			}
		}
		return new DwzResponse(DwzResponse.DwzResponseCode.FAIL, DEFAULT_VALIDATE_FAIL_MESSAGE, null, null, null);
	}

	public static DwzResponse createFailResponse(String message) {
		return new DwzResponse(DwzResponse.DwzResponseCode.FAIL, message, null, null, null);
	}

	public static DwzResponse createFailResponseWithForward(String forwardUrl) {
		return new DwzResponse(DwzResponse.DwzResponseCode.FAIL, DEFAULT_FAIL_MESSAGE,
				DwzResponse.CallbackType.FORWARD, forwardUrl, null);
	}

	public static DwzResponse createFailResponseWithForward(String message, String forwardUrl) {
		return new DwzResponse(DwzResponse.DwzResponseCode.FAIL, message, DwzResponse.CallbackType.FORWARD, forwardUrl,
				null);
	}
}
